<template>
  <div class="app-container">
    <div class="container">
      <!--工具栏-->
      <div style="margin-bottom:15px">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入名称搜索" size="small" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery"/>
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
        <el-button size="mini" icon="el-icon-refresh" @click="resetForm">重置</el-button>
        <!-- 新增 -->
        <div style="display: inline-block;margin: 0px 2px;">
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="add">新增</el-button>
        </div>
      </div>
      <!--表单组件-->
      <viewImg ref="viewImg" />
      <eForm ref="form" :is-add="isAdd"/>
      <!--表格渲染-->
      <el-table v-loading="loading" :data="data" :tree-props="{children: 'children', hasChildren: 'hasChildren'}" :default-expand-all="expand" row-key="id" size="small">
        <el-table-column label="页面标题" prop="page_title" align="center"/>
        <el-table-column prop="icon" label="图标" align="center">
          <template slot-scope="scope">
            <svg-icon v-if="scope.row.icon" :icon-class="scope.row.icon" />
          </template>
        </el-table-column>
        <el-table-column prop="path" label="相对路径" align="center"/>
        <el-table-column prop="preview_url" label="预览图" align="center">
          <template slot-scope="scope">
            <el-image :src="scope.row.preview_url" fit="cover" style="width: 100px;height: 100px" @click="viewImg(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建日期" align="center" >
          <template slot-scope="scope">
            <span>{{ parseTime(Number(scope.row.create_time)) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)"/>
            <el-popover
              v-permission="['ADMIN','MENU_ALL','MENU_DELETE']"
              :ref="scope.row.page_id"
              placement="top"
              width="200">
              <p>确定删除吗?</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="$refs[scope.row.page_id].doClose()">取消</el-button>
                <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.page_id)">确定</el-button>
              </div>
              <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"/>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        :total="total"
        :current-page="page + 1"
        background
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChange"
        @current-change="pageChange" />
    </div>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission' // 权限判断函数
import treeTable from '@/components/TreeTable'
import initData from '@/mixins/initData'
import { delPageById } from '@/api/mall/pageDecoration'
import { parseTime } from '@/utils/index'
import eForm from './form'
import viewImg from './viewImg'
export default {
  components: { treeTable, eForm, viewImg },
  mixins: [initData],
  data() {
    return {
      delLoading: false,
      expand: false,
      form: {
        page_id: ''
      }
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    checkPermission,
    beforeInit() {
      this.url = 'api/get-page-list'
      const query = this.query
      const value = query.value
      this.params = { page: this.page + 1, size: this.size }
      if (value) { this.params['page_title'] = value }
      return true
    },
    subDelete(id) {
      this.delLoading = true
      this.form.page_id = id
      delPageById(this.form).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err.response.data.message)
      })
    },
    add() {
      this.isAdd = true
      this.$refs.form.dialog = true
      this.$refs.form.resetForm()
    },
    edit(data) {
      this.isAdd = false
      const _this = this.$refs.form
      _this.form = {
        page_id: data.page_id,
        is_decoration: data.is_decoration + '',
        preview_url: data.preview_url,
        path: data.path,
        icon: data.icon,
        page_title: data.page_title
      }
      _this.dialog = true
    },
    resetForm() {
      this.query = {
        value: ''
      }
      this.init()
    },
    viewImg(row) {
      // 图片放大预览
      this.$refs.viewImg.dialog = true
      this.$refs.viewImg.url = row.preview_url
    },
    changExpand() {
      this.expand = !this.expand
      this.init()
    }
  }
}
</script>

<style scoped>

</style>
